package com.example.myapplication.found.model;

import com.example.myapplication.found.Stock;
import com.example.myapplication.found.bean.FoundBean;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.StockKlineBean2;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author: rongfeng
 * @date: 2022/8/19
 * @description
 */
public class Fenxi13 {
    public volatile static AtomicInteger dateIndex = new AtomicInteger(1000);
    public volatile static Vector<StockKlineBean2> lastList = new Vector<>();
    volatile static double money = 10000;
    public volatile static long mDate = 0;

    public void start(List<FoundBean> foundList) {
        System.out.println("dateIndex " + dateIndex);
        for (FoundBean foundBean : foundList) {
            Fenxi12.RangResult rangResult = Fenxi12.mRangResult.get(foundBean.f12);
            if (rangResult != null) {
                StockBean stockBean = Stock.stockBeans.get(foundBean.f12);

                List<StockKlineBean> klineList = stockBean.klineList.subList(0, stockBean.klineList.size() - Fenxi13.dateIndex.get());

                StockKlineBean bean = klineList.get(klineList.size() - 1);

                if (bean.date2 >= mDate &&
                        (bean.chg > rangResult.chgRange.start && bean.chg <= rangResult.chgRange.end) &&
                        (bean.zhenfu > rangResult.huanShourange.start && bean.zhenfu <= rangResult.huanShourange.end)) {

                    System.out.println(bean.date+" " + bean.chg + " : " + bean.huanshou);

                    if (klineList.size() < stockBean.klineList.size()) {
                        StockKlineBean lastBean = stockBean.klineList.get(klineList.size());

                        StockKlineBean2 stockKlineBean2 = new StockKlineBean2();
                        stockKlineBean2.chg = lastBean.chg;
                        stockKlineBean2.date = lastBean.date;
                        stockKlineBean2.zuoshou = bean.shouPan;
                        stockKlineBean2.code = foundBean.f12;
                        stockKlineBean2.name = foundBean.f14;
                        stockKlineBean2.huanshou = bean.huanshou;
                        stockKlineBean2.date2 = lastBean.date2;
                        lastList.add(stockKlineBean2);
                    }
                }
            }
        }
        analysis();
    }


    public void analysis() {
        double chgAvg = 0;
        int chgCount = 0;
        int zhang = 0;
        int die = 0;

        List<StockKlineBean2> dealList = new ArrayList<>();
        for (StockKlineBean2 stockKlineBean : lastList) {
            dealList.add(stockKlineBean);
            if (dealList.size() >= 4) {
                break;
            }
        }

        double itemMoney = money / dealList.size();
        double shouyi = 0;
        double dayShouyi = 0;
        String date = "";
        for (StockKlineBean2 stockKlineBean : dealList) {
            date = stockKlineBean.date;
            if (stockKlineBean.date2 > mDate) {
                mDate = stockKlineBean.date2;
            }
            int shou = (int) (itemMoney / (stockKlineBean.zuoshou * 100));
            if (shou <= 0) {
                continue;
            }
            double byMoney = shou * (stockKlineBean.zuoshou * 100);

            double shouxu = byMoney * 0.00022f;

            money = money - byMoney - shouxu;

            shouyi += byMoney + byMoney * (stockKlineBean.chg / 100f);

            double shouxu3 = shouyi * 0.00022f;

            shouyi = shouyi - shouxu3;

            chgAvg += stockKlineBean.chg;
            chgCount++;
            if (stockKlineBean.chg > 0) {
                zhang++;
            } else if (stockKlineBean.chg < 0) {
                die++;
            }

            System.out.println(stockKlineBean.code + " " + stockKlineBean.name + " " + stockKlineBean.chg + " : " + stockKlineBean.huanshou);

        }
        money += shouyi;

        System.out.println(date);
        System.out.println("余额 " + money);
        System.out.println();

        lastList.clear();
    }


}
